/**
 * 
 */
package org.unisannio;

import java.util.Iterator;
import java.util.Set;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

/**
 * @author sistemiembedded2011
 * 
 */
public class ClientReceiver extends BroadcastReceiver {

	private static final String ACTION = "org.unisannio.PolitePingPong";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent != null && intent.getAction() != null
				&& ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
			String nostraStringa;
			Bundle bundle = getResultExtras(true);
			String bundleString = bundle.toString();
			// nostraStringa=bundle.getString("stringaInviata");
			// nostraStringa=bundle.getString("risposta");
			Set<String> chiavi = bundle.keySet();
			String current;
			String objecString;
			Object obj;
			Class clas;
			// DA OTTIMIZZARE - CODICE DUPLICATO INSERIRLO IN UN METODO
			for (Iterator iterator = chiavi.iterator(); iterator.hasNext();) {
				current = (String) iterator.next();
				obj = bundle.get(current);
				clas = obj.getClass();
				Class type = clas.getComponentType();
				if (clas.isArray()) {
					objecString = this.naviga(obj);
				} else
					objecString = obj.toString();

				// if (obj instanceof Object[]) {
				// Object[] array = (Object[]) obj;
				// String res = String.valueOf(array[0]);
				// for (int i = 1; i < array.length; i++) {
				// res += ", " + array[i];
				// }
				// objecString = res;
				// } else if (obj instanceof String[]) {
				// String[] array = (String[]) obj;
				// String res = String.valueOf(array[0]);
				// for (int i = 1; i < array.length; i++) {
				// res += ", " + array[i];
				// }
				// objecString = res;
				// } else if (obj instanceof double[]) {
				// double[] array = (double[]) obj;
				// String res = String.valueOf(array[0]);
				// for (int i = 1; i < array.length; i++) {
				// res += ", " + array[i];
				// }
				// objecString = res;
				// } else
				// objecString = obj.toString();
				// clas.cast(nn);
				// System.out.println(nns);
				Toast toast = Toast.makeText(context,
						current + ":" + clas.toString() + "  tipo:" + type
								+ " " + objecString, Toast.LENGTH_LONG);
				toast.show();
			}

			String[] array = bundle.getStringArray("risposte");
			for (int i = 0; i < array.length; i++) {
				Toast toast = Toast.makeText(context, "Client[" + i + "] "
						+ array[i], Toast.LENGTH_LONG);
				toast.show();
			}

		}
	}

	//questo metodo non mi piace ma non so come ottimizzarlo per i primitivi
	private String naviga(Object obj) {
		String result="";
		Class clas = obj.getClass();
		Class type = clas.getComponentType();
		if (type.toString().equalsIgnoreCase("int")){
			int[] array = (int[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		else if (type.toString().equalsIgnoreCase("char")){
			char[] array = (char[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		else if (type.toString().equalsIgnoreCase("double")){
			double[] array = (double[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		else if (type.toString().equalsIgnoreCase("float")){
			float[] array = (float[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		else if (type.toString().equalsIgnoreCase("long")){
			long[] array = (long[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		else if (type.toString().equalsIgnoreCase("short")){
			short[] array = (short[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		else {
			Object[] array = (Object[]) obj;
			result = String.valueOf(array[0]);
			for (int i = 1; i < array.length; i++) {
				result += ", " + array[i];
			}
		}
		return result;
	}

}
